	
	/******************************************************************************\
	|    Title:        	 Replication code for Globalization, Government            |
	|                    Popularity, and the Great Skill Divide     			   |
	|    Date:         	 September 2023                                            |
	|	 Description:    Trade data cleaning and IV construction  				   |
	\******************************************************************************/

	clear all
	clear matrix
	clear mata

	set matsize 11000, perm
	set maxvar 32767

	* Define data and other paths:
	global savet "3-replication-package/3-tables"
	global savef "3-replication-package/4-figures"
	
	gl conf "4-confidential-data-not-for-publication"
	gl data "3-replication-package/1-data"

****************************************************************
**# Create aggregated datasets from raw data
****************************************************************
	
	**# Exports
	*****************************************************************

	* Notes: I classify the industries as agriculture, low technology, medium technology, high technology using the definitions provided 
	* by the UNCTADSTAT @ http://unctadstat.unctad.org/EN/Classifications.html
	* Agriculture industries are treated as homogenous, therefore, I cannot separate more and less-skill intensive agriculture exports.

	* Load data

	use "$data/2005-2016 (Export) - All 3-digit SITC Rev.3 Commodities.dta"

	* Agricultural goods
	* Agricultural sector SITC codes are 0,1,2 and 4

	gen agriculture=1 if commoditycode==1

	forvalues x = 1(1)299 { 
	replace agriculture=1 if commoditycode==`x'
	}

	forvalues x = 400(1)499 { 
	replace agriculture=1 if commoditycode==`x'
	}

	label var agriculture "Agricultural exports"

	**************************************
	* Classifications by SKILL INTENSITY *
	**************************************

	* TDRB	Labour-intensive and resource-intensive manufactures

	gen labourintensive=1 if commoditycode==611		// Leather
	replace labourintensive=1 if commoditycode==612	// Manufactures of leather, n.e.s.; saddlery & harness
	replace labourintensive=1 if commoditycode==613	// Furskins, tanned or dressed, excluding those of 8483
	replace labourintensive=1 if commoditycode==633	// Cork manufactures
	replace labourintensive=1 if commoditycode==634	// Veneers, plywood, and other wood, worked, n.e.s.
	replace labourintensive=1 if commoditycode==635	// Wood manufacture, n.e.s.
	replace labourintensive=1 if commoditycode==641	// Paper and paperboard
	replace labourintensive=1 if commoditycode==642	// Paper & paperboard, cut to shape or size, articles
	replace labourintensive=1 if commoditycode==651	// Textile yarn
	replace labourintensive=1 if commoditycode==652	// Cotton fabrics, woven
	replace labourintensive=1 if commoditycode==653	// Fabrics, woven, of man-made fabrics
	replace labourintensive=1 if commoditycode==654	// Other textile fabrics, woven
	replace labourintensive=1 if commoditycode==655	// Knitted or crocheted fabrics, n.e.s.
	replace labourintensive=1 if commoditycode==656	// Tulles, trimmings, lace, ribbons & other small wares
	replace labourintensive=1 if commoditycode==657 // Special yarn, special textile fabrics & related
	replace labourintensive=1 if commoditycode==658	// Made-up articles, of textile materials, n.e.s.
	replace labourintensive=1 if commoditycode==659	// Floor coverings, etc.
	replace labourintensive=1 if commoditycode==661	// Lime, cement, fabrica. constr. mat. (excludingglass, clay)
	replace labourintensive=1 if commoditycode==662	// Clay construction, refracto. construction materials
	replace labourintensive=1 if commoditycode==663	// Mineral manufactures, n.e.s.
	replace labourintensive=1 if commoditycode==664	// Glass
	replace labourintensive=1 if commoditycode==665	// Glassware
	replace labourintensive=1 if commoditycode==666	// Pottery
	replace labourintensive=1 if commoditycode==821	// Furniture & parts
	replace labourintensive=1 if commoditycode==831	// Travel goods, handbags & similar containers
	replace labourintensive=1 if commoditycode==841	// Men's clothing of textile fabrics, not knitted
	replace labourintensive=1 if commoditycode==842	// Women's clothing, of textile fabrics
	replace labourintensive=1 if commoditycode==843	// Men's or boy's clothing, of textile, knitted, croche.
	replace labourintensive=1 if commoditycode==844	// Women's clothing, of textile, knitted or crocheted
	replace labourintensive=1 if commoditycode==845	// Articles of apparel, of textile fabrics, n.e.s.
	replace labourintensive=1 if commoditycode==846	// Clothing accessories, of textile fabrics
	replace labourintensive=1 if commoditycode==848	// Articles of apparel, clothing access., excluding textile
	replace labourintensive=1 if commoditycode==851	// Footwear

	label var labourintensive "Labour intensive exports"

	* TDRC	Low-skill and technology-intensive manufactures

	gen lowskill=1 if commoditycode==671			// Pig iron & spiegeleisen, sponge iron, powder & granu
	replace lowskill=1 if commoditycode==672		// Ingots, primary forms, of iron or steel; semi-finis.
	replace lowskill=1 if commoditycode==673		// Flat-rolled prod., iron, non-alloy steel, not coated
	replace lowskill=1 if commoditycode==674		// Flat-rolled prod., iron, non-alloy steel, coated, clad
	replace lowskill=1 if commoditycode==675		// Flat-rolled products of alloy steel
	replace lowskill=1 if commoditycode==676		// Iron & steel bars, rods, angles, shapes & sections
	replace lowskill=1 if commoditycode==677		// Rails & railway track construction mat., iron, steel
	replace lowskill=1 if commoditycode==678		// Wire of iron or steel
	replace lowskill=1 if commoditycode==679		// Tubes, pipes & hollow profiles, fittings, iron, steel
	replace lowskill=1 if commoditycode==691		// Structures & parts, n.e.s., of iron, steel, aluminium
	replace lowskill=1 if commoditycode==692		// Metal containers for storage or transport
	replace lowskill=1 if commoditycode==693		// Wire products (excluding electrical) and fencing grills
	replace lowskill=1 if commoditycode==694		// Nails, screws, nuts, bolts, rivets & the like, of metal
	replace lowskill=1 if commoditycode==695		// Tools for use in the hand or in machine
	replace lowskill=1 if commoditycode==696		// Cutlery
	replace lowskill=1 if commoditycode==697		// Household equipment of base metal, n.e.s.
	replace lowskill=1 if commoditycode==699		// Manufactures of base metal, n.e.s.
	replace lowskill=1 if commoditycode==785		// Motorcycles & cycles
	replace lowskill=1 if commoditycode==786		// Trailers & semi-trailers
	replace lowskill=1 if commoditycode==791		// Railway vehicles & associated equipment
	replace lowskill=1 if commoditycode==793		// Ships, boats & floating structures
	replace lowskill=1 if commoditycode==895		// Office & stationery supplies, n.e.s.
	replace lowskill=1 if commoditycode==899		// Miscellaneous manufactured articles, n.e.s.

	label var lowskill "Low-skill intensive exports"

	* TDRD	Medium-skill and technology-intensive manufactures
	* TDRD1	Medium-skill: Electronics (excluding parts and components) (SITC 775)

	gen mediumskill=1 if commoditycode==775			// Household type equipment, electrical or not, n.e.s.

	* TDRD2	Medium-skill: Parts and components for electrical and electronic goods (SITC 772)

	replace mediumskill=1 if commoditycode==772		// Apparatus for electrical circuits; board, panels

	* TDRD3	Medium-skill: Other, excluding electronics

	replace mediumskill=1 if commoditycode==621		// Materials of rubber (pastes, plates, sheets, etc.)
	replace mediumskill=1 if commoditycode==625		// Rubber tyres, tyre treads or flaps & inner tubes
	replace mediumskill=1 if commoditycode==629		// Articles of rubber, n.e.s.
	replace mediumskill=1 if commoditycode==711		// Vapour generating boilers, auxiliary plant; parts
	replace mediumskill=1 if commoditycode==712		// Steam turbines & other vapour turbin., parts, n.e.s.
	replace mediumskill=1 if commoditycode==713		// Internal combustion piston engines, parts, n.e.s.
	replace mediumskill=1 if commoditycode==714		// Engines & motors, non-electric; parts, n.e.s.
	replace mediumskill=1 if commoditycode==716		// Rotating electric plant & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==718		// Other power generating machinery & parts, n.e.s.
	replace mediumskill=1 if commoditycode==721		// Agricultural machinery (excluding tractors) & parts
	replace mediumskill=1 if commoditycode==722		// Tractors (excluding those of 71414 & 74415)
	replace mediumskill=1 if commoditycode==723		// Civil engineering & contractors' plant & equipment
	replace mediumskill=1 if commoditycode==724		// Textile & leather machinery, & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==725		// Paper mill, pulp mill machinery; paper articles man.
	replace mediumskill=1 if commoditycode==726		// Printing & bookbinding machinery, & parts thereof
	replace mediumskill=1 if commoditycode==727		// Food-processing machines (excluding domestic)
	replace mediumskill=1 if commoditycode==728		// Other machinery for particular industries, n.e.s.
	replace mediumskill=1 if commoditycode==731		// Machine-tools working by removing material
	replace mediumskill=1 if commoditycode==733		// Mach.-tools for working metal, excluding removing mate.
	replace mediumskill=1 if commoditycode==735		// Parts, n.e.s., & accessories for machines of 731, 733
	replace mediumskill=1 if commoditycode==737		// Metalworking machinery (excludingmachine-tools) & parts
	replace mediumskill=1 if commoditycode==741		// Heating & cooling equipment & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==742		// Pumps for liquids
	replace mediumskill=1 if commoditycode==743		// Pumps (excluding liquid), gas compressors & fans; centr.
	replace mediumskill=1 if commoditycode==744		// Mechanical handling equipment, & parts, n.e.s.
	replace mediumskill=1 if commoditycode==745		// Other non-electr. machinery, tools & mechan. appar.
	replace mediumskill=1 if commoditycode==746		// Ball or roller bearings
	replace mediumskill=1 if commoditycode==747		// Appliances for pipes, boiler shells, tanks, vats, etc.
	replace mediumskill=1 if commoditycode==748		// Transmis. shafts
	replace mediumskill=1 if commoditycode==749		// Non-electric parts & accessor. of machinery, n.e.s.
	replace mediumskill=1 if commoditycode==771		// Electric power machinery, and parts thereof
	replace mediumskill=1 if commoditycode==773		// Equipment for distributing electricity, n.e.s.
	replace mediumskill=1 if commoditycode==774		// Electro-diagnostic appa. for medical sciences, etc.
	replace mediumskill=1 if commoditycode==778		// Electrical machinery & apparatus, n.e.s.
	replace mediumskill=1 if commoditycode==781		// Motor vehicles for the transport of persons
	replace mediumskill=1 if commoditycode==782		// Motor vehic. for transport of goods, special purpo.
	replace mediumskill=1 if commoditycode==783		// Road motor vehicles, n.e.s.
	replace mediumskill=1 if commoditycode==784		// Parts & accessories of vehicles of 722, 781, 782, 783
	replace mediumskill=1 if commoditycode==811		// Prefabricated buildings
	replace mediumskill=1 if commoditycode==812		// Sanitary, plumbing, heating fixtures, fittings, n.e.s.
	replace mediumskill=1 if commoditycode==813		// Lighting fixtures & fittings, n.e.s.
	replace mediumskill=1 if commoditycode==893		// Articles, n.e.s., of plastics
	replace mediumskill=1 if commoditycode==894		// Baby carriages, toys, games & sporting goods

	label var mediumskill "Medium-skill intensive exports"

	* TDRE	High-skill and technology-intensive manufactures
	* TDRE1	High-skill: Electronics (excluding parts and components) (SITC 751 + 752 + 761 + 762 + 763)

	gen highskill=1 if commoditycode==751		// Office machines
	replace highskill=1 if commoditycode==752	// Automatic data processing machines, n.e.s.
	replace highskill=1 if commoditycode==761	// Television receivers, whether or not combined
	replace highskill=1 if commoditycode==762	// Radio-broadcast receivers, whether or not combined
	replace highskill=1 if commoditycode==763	// Sound recorders or reproducers

	* TDRE2	High-skill: Parts and components for electrical and electronic goods (SITC 759 + 764 + 776)

	replace highskill=1 if commoditycode==759	// Parts, accessories for machines of groups 751, 752
	replace highskill=1 if commoditycode==764	// Telecommunication equipment, n.e.s.; & parts, n.e.s.
	replace highskill=1 if commoditycode==776	// Cathode valves & tubes

	* TDRE3	High-skill: Other, excluding electronics

	replace highskill=1 if commoditycode==511	// Hydrocarbons, n.e.s., & halogenated, nitr. derivative
	replace highskill=1 if commoditycode==512	// Alcohols, phenols, halogenat., sulfonat., nitrat. der.
	replace highskill=1 if commoditycode==513	// Carboxylic acids, anhydrides, halides, per.; derivati.
	replace highskill=1 if commoditycode==514	// Nitrogen-function compounds
	replace highskill=1 if commoditycode==515	// Organo-inorganic, heterocycl. compounds, nucl. acids
	replace highskill=1 if commoditycode==516	// Other organic chemicals
	replace highskill=1 if commoditycode==522	// Inorganic chemical elements, oxides & halogen salts
	replace highskill=1 if commoditycode==523	// Metallic salts & peroxysalts, of inorganic acids
	replace highskill=1 if commoditycode==524	// Other inorganic chemicals
	replace highskill=1 if commoditycode==525	// Radio-actives and associated materials
	replace highskill=1 if commoditycode==531	// Synth. organic colouring matter & colouring lakes
	replace highskill=1 if commoditycode==532	// Dyeing & tanning extracts, synth. tanning materials
	replace highskill=1 if commoditycode==533	// Pigments, paints, varnishes and related materials
	replace highskill=1 if commoditycode==541	// Medicinal and pharmaceutical products, excluding 542
	replace highskill=1 if commoditycode==542	// Medicaments (incl. veterinary medicaments)
	replace highskill=1 if commoditycode==551	// Essential oils, perfume & flavour materials
	replace highskill=1 if commoditycode==553	// Perfumery, cosmetics or toilet prepar. (excluding soaps)
	replace highskill=1 if commoditycode==554	// Soaps, cleansing and polishing preparations
	replace highskill=1 if commoditycode==562	// Fertilizers (other than those of group 272)
	replace highskill=1 if commoditycode==571	// Polymers of ethylene, in primary forms
	replace highskill=1 if commoditycode==572	// Polymers of styrene, in primary forms
	replace highskill=1 if commoditycode==573	// Polymers of vinyl chloride or halogenated olefins
	replace highskill=1 if commoditycode==574	// Polyethers, epoxide resins; polycarbonat., polyesters
	replace highskill=1 if commoditycode==575	// Other plastics, in primary forms
	replace highskill=1 if commoditycode==579	// Waste, parings and scrap, of plastics
	replace highskill=1 if commoditycode==581	// Tubes, pipes and hoses of plastics
	replace highskill=1 if commoditycode==582	// Plates, sheets, films, foil & strip, of plastics
	replace highskill=1 if commoditycode==583	// Monofilaments, of plastics, cross-section > 1mm
	replace highskill=1 if commoditycode==591	// Insectides &  similar products, for retail sale
	replace highskill=1 if commoditycode==592	// Starche, wheat gluten; albuminoidal substances; glues
	replace highskill=1 if commoditycode==593	// Explosives and pyrotechnic products
	replace highskill=1 if commoditycode==597	// Prepared addit. for miner. oils; lubricat., de-icing
	replace highskill=1 if commoditycode==598	// Miscellaneous chemical products, n.e.s.
	replace highskill=1 if commoditycode==792	// Aircraft & associated equipment; spacecraft, etc.
	replace highskill=1 if commoditycode==871	// Optical instruments & apparatus, n.e.s.
	replace highskill=1 if commoditycode==872	// Instruments & appliances, n.e.s., for medical, etc.
	replace highskill=1 if commoditycode==873	// Meters & counters, n.e.s.
	replace highskill=1 if commoditycode==874	// Measuring, analysing & controlling apparatus, n.e.s.
	replace highskill=1 if commoditycode==881	// Photographic apparatus & equipment, n.e.s.
	replace highskill=1 if commoditycode==882	// Cinematographic & photographic supplies
	replace highskill=1 if commoditycode==883	// Cinematograph films, exposed & developed
	replace highskill=1 if commoditycode==884	// Optical goods, n.e.s.
	replace highskill=1 if commoditycode==885	// Watches & clocks
	replace highskill=1 if commoditycode==891	// Arms & ammunition
	replace highskill=1 if commoditycode==892	// Printed matter
	replace highskill=1 if commoditycode==896	// Works of art, collectors' pieces & antiques
	replace highskill=1 if commoditycode==897	// Jewellery & articles of precious materia., n.e.s.
	replace highskill=1 if commoditycode==898	// Musical instruments, parts; records, tapes & similar

	label var highskill "High-skill intensive exports"

	* Create stata_id for each country to match with GALLUP data

	rename country country_gallup

	* Code for matching country names

	gen stata_id=. 	
	replace stata_id=1 		if country_gallup=="Afghanistan"
	replace stata_id=2	 	if country_gallup=="Albania"
	replace stata_id=3		if country_gallup=="Algeria"
	replace stata_id=4		if country_gallup=="Angola"
	replace stata_id=5		if country_gallup=="Argentina"
	replace stata_id=6		if country_gallup=="Armenia"
	replace stata_id=7		if country_gallup=="Australia"
	replace stata_id=8		if country_gallup=="Austria"
	replace stata_id=9		if country_gallup=="Azerbaijan"
	replace stata_id=10		if country_gallup=="Bahamas, The"
	replace stata_id=11		if country_gallup=="Bahrain"
	replace stata_id=12		if country_gallup=="Bangladesh"
	replace stata_id=13		if country_gallup=="Barbados"
	replace stata_id=14		if country_gallup=="Belarus"
	replace stata_id=15		if country_gallup=="Belgium"
	replace stata_id=16		if country_gallup=="Belize"
	replace stata_id=17		if country_gallup=="Benin"
	replace stata_id=18		if country_gallup=="Bhutan"
	replace stata_id=19		if country_gallup=="Bolivia"
	replace stata_id=21		if country_gallup=="Bosnia and Herzegovina" | country_gallup=="Bosnia Herzegovina"
	replace stata_id=22		if country_gallup=="Botswana"
	replace stata_id=23		if country_gallup=="Brazil"
	replace stata_id=24		if country_gallup=="Brunei Darussalam"
	replace stata_id=25		if country_gallup=="Bulgaria"
	replace stata_id=26		if country_gallup=="Burkina Faso"
	replace stata_id=27		if country_gallup=="Burundi"
	replace stata_id=28		if country_gallup=="Cabo Verde"
	replace stata_id=29		if country_gallup=="Cambodia"
	replace stata_id=30		if country_gallup=="Cameroon"
	replace stata_id=31		if country_gallup=="Canada"
	replace stata_id=32		if country_gallup=="Cape Verde"
	replace stata_id=33		if country_gallup=="Central African Republic" | country_gallup=="Central African Rep."
	replace stata_id=34		if country_gallup=="Chad"
	replace stata_id=35		if country_gallup=="Chile"
	replace stata_id=36		if country_gallup=="China"
	replace stata_id=37		if country_gallup=="Colombia"
	replace stata_id=38		if country_gallup=="Comoros"
	replace stata_id=39		if country_gallup=="Congo Brazzaville"
	replace stata_id=40		if country_gallup=="Congo Kinshasa"
	replace stata_id=41		if country_gallup=="Costa Rica"
	replace stata_id=42		if country_gallup=="Croatia"
	replace stata_id=43		if country_gallup=="Cuba"
	replace stata_id=44		if country_gallup=="Cyprus"
	replace stata_id=45		if country_gallup=="Czech Republic"
	replace stata_id=46		if country_gallup=="Denmark"
	replace stata_id=47		if country_gallup=="Djibouti"
	replace stata_id=48		if country_gallup=="Dominican Republic" | country_gallup=="Dominican Rep."
	replace stata_id=49		if country_gallup=="East Timor"
	replace stata_id=50		if country_gallup=="Ecuador"
	replace stata_id=51		if country_gallup=="Egypt"
	replace stata_id=52		if country_gallup=="El Salvador"
	replace stata_id=53		if country_gallup=="Equatorial Guinea"
	replace stata_id=54		if country_gallup=="Eritrea"
	replace stata_id=55		if country_gallup=="Estonia"
	replace stata_id=56		if country_gallup=="Ethiopia"
	replace stata_id=58		if country_gallup=="Fiji"
	replace stata_id=59		if country_gallup=="Finland"
	replace stata_id=60		if country_gallup=="France"
	replace stata_id=61		if country_gallup=="Gabon"
	replace stata_id=62		if country_gallup=="Gambia"
	replace stata_id=63		if country_gallup=="Georgia"
	replace stata_id=64		if country_gallup=="Germany"
	replace stata_id=65		if country_gallup=="Ghana"
	replace stata_id=66		if country_gallup=="Greece"
	replace stata_id=67		if country_gallup=="Guatemala"
	replace stata_id=68		if country_gallup=="Guinea"
	replace stata_id=70		if country_gallup=="Guyana"
	replace stata_id=71		if country_gallup=="Haiti"
	replace stata_id=72		if country_gallup=="Honduras"
	replace stata_id=73		if country_gallup=="Hong Kong"
	replace stata_id=75		if country_gallup=="Hungary"
	replace stata_id=76		if country_gallup=="Iceland"
	replace stata_id=77		if country_gallup=="India"
	replace stata_id=78		if country_gallup=="Indonesia"
	replace stata_id=79		if country_gallup=="Iran"
	replace stata_id=80		if country_gallup=="Iraq"
	replace stata_id=81		if country_gallup=="Ireland"
	replace stata_id=82		if country_gallup=="Israel"
	replace stata_id=83		if country_gallup=="Italy"
	replace stata_id=84		if country_gallup=="Ivory Coast" | country_gallup=="CÃ´te d'Ivoire"
	replace stata_id=85		if country_gallup=="Jamaica"
	replace stata_id=86		if country_gallup=="Japan"
	replace stata_id=87		if country_gallup=="Jordan"
	replace stata_id=88		if country_gallup=="Kazakhstan"
	replace stata_id=89		if country_gallup=="Kenya"
	replace stata_id=91		if country_gallup=="Korea North"
	replace stata_id=92		if country_gallup=="Kosovo"
	replace stata_id=93		if country_gallup=="Kuwait"
	replace stata_id=95		if country_gallup=="Kyrgyzstan" | country_gallup=="Kyrgyz Republic"
	replace stata_id=96		if country_gallup=="Laos"
	replace stata_id=97		if country_gallup=="Latvia"
	replace stata_id=98		if country_gallup=="Lebanon"
	replace stata_id=99		if country_gallup=="Lesotho"
	replace stata_id=100	if country_gallup=="Liberia"
	replace stata_id=101	if country_gallup=="Libya"
	replace stata_id=102	if country_gallup=="Lithuania"
	replace stata_id=103	if country_gallup=="Luxembourg"
	replace stata_id=104	if country_gallup=="Macao SAR"
	replace stata_id=106	if country_gallup=="Macedonia, FYR"
	replace stata_id=107	if country_gallup=="Madagascar"
	replace stata_id=108	if country_gallup=="Malawi"
	replace stata_id=109	if country_gallup=="Malaysia"
	replace stata_id=110	if country_gallup=="Mali"
	replace stata_id=111	if country_gallup=="Malta"
	replace stata_id=112	if country_gallup=="Mauritania"
	replace stata_id=113	if country_gallup=="Mauritius"
	replace stata_id=114	if country_gallup=="Mexico"
	replace stata_id=115	if country_gallup=="Moldova"
	replace stata_id=116	if country_gallup=="Mongolia"
	replace stata_id=117	if country_gallup=="Montenegro"
	replace stata_id=118	if country_gallup=="Morocco"
	replace stata_id=119	if country_gallup=="Mozambique"
	replace stata_id=120	if country_gallup=="Myanmar"
	replace stata_id=122	if country_gallup=="Nagorno Karabakh"
	replace stata_id=123	if country_gallup=="Namibia"
	replace stata_id=124	if country_gallup=="Nepal"
	replace stata_id=125	if country_gallup=="Netherlands"
	replace stata_id=126	if country_gallup=="New Zealand"
	replace stata_id=127	if country_gallup=="Nicaragua"
	replace stata_id=128	if country_gallup=="Niger"
	replace stata_id=129	if country_gallup=="Nigeria"
	replace stata_id=130	if country_gallup=="Northern Cyprus"
	replace stata_id=131	if country_gallup=="Norway"
	replace stata_id=132	if country_gallup=="Oman"
	replace stata_id=133	if country_gallup=="Pakistan"
	replace stata_id=134	if country_gallup=="Palestine" | country_gallup=="State of Palestine"
	replace stata_id=135	if country_gallup=="Panama"
	replace stata_id=136	if country_gallup=="Papua New Guinea"
	replace stata_id=137	if country_gallup=="Paraguay"
	replace stata_id=138	if country_gallup=="Peru"
	replace stata_id=139	if country_gallup=="Philippines"
	replace stata_id=140	if country_gallup=="Poland"
	replace stata_id=141	if country_gallup=="Portugal"
	replace stata_id=142	if country_gallup=="Puerto Rico"
	replace stata_id=143	if country_gallup=="Qatar"
	replace stata_id=144	if country_gallup=="Romania"
	replace stata_id=145	if country_gallup=="Russia"
	replace stata_id=146	if country_gallup=="Rwanda"
	replace stata_id=147	if country_gallup=="San Marino"
	replace stata_id=148	if country_gallup=="Saudi Arabia"
	replace stata_id=149	if country_gallup=="Senegal"
	replace stata_id=150	if country_gallup=="Serbia"
	replace stata_id=151	if country_gallup=="Serbia and Montenegro"
	replace stata_id=152	if country_gallup=="Seychelles"
	replace stata_id=153	if country_gallup=="Sierra Leone"
	replace stata_id=154	if country_gallup=="Singapore"
	replace stata_id=155	if country_gallup=="Slovak Republic"
	replace stata_id=157	if country_gallup=="Slovenia"
	replace stata_id=158	if country_gallup=="Solomon Islands"
	replace stata_id=159	if country_gallup=="Somalia"
	replace stata_id=161	if country_gallup=="South Africa"
	replace stata_id=162	if country_gallup=="South Korea" | country_gallup=="Rep. of Korea"
	replace stata_id=163	if country_gallup=="South Sudan"
	replace stata_id=164	if country_gallup=="Spain"
	replace stata_id=165	if country_gallup=="Sri Lanka"
	replace stata_id=166	if country_gallup=="Sudan"
	replace stata_id=168	if country_gallup=="Suriname"
	replace stata_id=169	if country_gallup=="Swaziland"
	replace stata_id=170	if country_gallup=="Sweden"
	replace stata_id=171	if country_gallup=="Switzerland"
	replace stata_id=172	if country_gallup=="Syria"
	replace stata_id=173	if country_gallup=="S䯠Tom顡nd Pripe"
	replace stata_id=174	if country_gallup=="Taiwan"
	replace stata_id=175	if country_gallup=="Taiwan Province of China"
	replace stata_id=176	if country_gallup=="Tajikistan"
	replace stata_id=177	if country_gallup=="Tanzania" | country_gallup=="United Rep. of Tanzania"
	replace stata_id=178	if country_gallup=="Thailand"
	replace stata_id=179	if country_gallup=="Togo"
	replace stata_id=180	if country_gallup=="Trinidad and Tobago"
	replace stata_id=181	if country_gallup=="Tunisia"
	replace stata_id=182	if country_gallup=="Turkey"
	replace stata_id=183	if country_gallup=="Turkmenistan"
	replace stata_id=184	if country_gallup=="UAE"
	replace stata_id=185	if country_gallup=="Uganda"
	replace stata_id=186	if country_gallup=="Ukraine"
	replace stata_id=187	if country_gallup=="United Arab Emirates"
	replace stata_id=188	if country_gallup=="United Kingdom" 
	replace stata_id=189	if country_gallup=="United States of America" | country_gallup=="USA"
	replace stata_id=190	if country_gallup=="Uruguay"
	replace stata_id=191	if country_gallup=="Uzbekistan"
	replace stata_id=192	if country_gallup=="Venezuela"
	replace stata_id=193	if country_gallup=="Viet Nam"
	replace stata_id=194	if country_gallup=="Yemen"
	replace stata_id=195	if country_gallup=="Zambia"
	replace stata_id=196	if country_gallup=="Zimbabwe"

	* Detect unmatched stata_id and country names

	tab country_gallup if stata_id==.

	replace stata_id=10		if country_gallup=="Bahamas"
	replace stata_id=19		if country_gallup=="Bolivia (Plurinational State of)"
	replace stata_id=39		if country_gallup=="Congo"
	replace stata_id=40		if country_gallup=="Democratic Republic of the Congo"
	replace stata_id=84		if country_gallup=="Côte d'Ivoire"
	replace stata_id=51		if country_gallup=="Egypt, Arab Rep."
	replace stata_id=62		if country_gallup=="Gambia, The"
	replace stata_id=68		if country_gallup=="Guinea-Bissau"
	replace stata_id=73		if country_gallup=="China, Hong Kong SAR"
	replace stata_id=79		if country_gallup=="Iran (Islamic Republic of)"
	replace stata_id=91		if country_gallup=="Dem. People's Republic of Korea"
	replace stata_id=162	if country_gallup=="Republic of Korea"
	replace stata_id=96		if country_gallup=="Lao People's Democratic Republic"
	replace stata_id=104	if country_gallup=="China, Macao SAR"
	replace stata_id=106	if country_gallup=="TFYR Macedonia" | country_gallup=="TFYR of Macedonia"
	replace stata_id=115	if country_gallup=="Rep. of Moldova"
	replace stata_id=145	if country_gallup=="Russian Federation"
	replace stata_id=155	if country_gallup=="Slovakia"
	replace stata_id=173	if country_gallup=="Sao Tome and Principe"
	replace stata_id=172	if country_gallup=="Syrian Arab Republic"
	replace stata_id=192	if country_gallup=="Venezuela (Bolivarian Republic of)"
	replace stata_id=194	if country_gallup=="Yemen, Rep."

	tab stata_id

	drop if stata_id==.

	******************************************************
	* Calculate annual export volumes by skill composition
	******************************************************

	* Labour intensive exports

	collapse (sum) exportvalue if labourintensive==1, by (country_gallup year stata_id)

	rename exportvalue total_labour_int_exp
	label var total_labour_int_exp "Total Labour Intensive Exports" 

	save "$data/2002_2018_labourintensive_exports_country_level.dta", replace

	* Low-skill intensive exports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) exportvalue if lowskill==1, by (country_gallup year stata_id)

	rename exportvalue total_lowskill_int_exp
	label var total_lowskill_int_exp "Total Low-Skill Intensive Exports" 

	save "$data/2002_2018_lowskilled_exports_country_level.dta", replace

	* Medium-skill intensive exports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) exportvalue if mediumskill==1, by (country_gallup year stata_id)

	rename exportvalue total_mediumskill_int_exp
	label var total_mediumskill_int_exp "Total Medium-Skill Intensive Exports" 

	save "$data/2002_2018_mediumskilled_exports_country_level.dta", replace

	* High-skill intensive exports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) exportvalue if highskill==1, by (country_gallup year stata_id)

	rename exportvalue total_highskill_int_exp
	label var total_highskill_int_exp "Total High-Skill Intensive Exports" 

	save "$data/2002_2018_highskilled_exports_country_level.dta", replace

	
	**# Imports
	*****************************************************************
	
		* Notes: I classify the industries as agriculture, low technology, medium technology, high technology using the definitions provided 
	* by the UNCTADSTAT @ http://unctadstat.unctad.org/EN/Classifications.html
	* Agriculture industries are treated as homogenous, therefore, I cannot separate more and less-skill intensive agriculture imports.

	* Load data

	use "$data/2005-2016 (Import) - All 3-digit SITC Rev.3 Commodities.dta"

	* Agricultural goods
	* Agricultural sector SITC codes are 0,1,2 and 4

	gen agriculture=1 if commoditycode==1

	forvalues x = 1(1)299 { 
	replace agriculture=1 if commoditycode==`x'
	}

	forvalues x = 400(1)499 { 
	replace agriculture=1 if commoditycode==`x'
	}

	label var agriculture "Agricultural imports"

	**************************************
	* Classifications by SKILL INTENSITY *
	**************************************

	* TDRB	Labour-intensive and resource-intensive manufactures

	gen labourintensive=1 if commoditycode==611		// Leather
	replace labourintensive=1 if commoditycode==612	// Manufactures of leather, n.e.s.; saddlery & harness
	replace labourintensive=1 if commoditycode==613	// Furskins, tanned or dressed, excluding those of 8483
	replace labourintensive=1 if commoditycode==633	// Cork manufactures
	replace labourintensive=1 if commoditycode==634	// Veneers, plywood, and other wood, worked, n.e.s.
	replace labourintensive=1 if commoditycode==635	// Wood manufacture, n.e.s.
	replace labourintensive=1 if commoditycode==641	// Paper and paperboard
	replace labourintensive=1 if commoditycode==642	// Paper & paperboard, cut to shape or size, articles
	replace labourintensive=1 if commoditycode==651	// Textile yarn
	replace labourintensive=1 if commoditycode==652	// Cotton fabrics, woven
	replace labourintensive=1 if commoditycode==653	// Fabrics, woven, of man-made fabrics
	replace labourintensive=1 if commoditycode==654	// Other textile fabrics, woven
	replace labourintensive=1 if commoditycode==655	// Knitted or crocheted fabrics, n.e.s.
	replace labourintensive=1 if commoditycode==656	// Tulles, trimmings, lace, ribbons & other small wares
	replace labourintensive=1 if commoditycode==657 // Special yarn, special textile fabrics & related
	replace labourintensive=1 if commoditycode==658	// Made-up articles, of textile materials, n.e.s.
	replace labourintensive=1 if commoditycode==659	// Floor coverings, etc.
	replace labourintensive=1 if commoditycode==661	// Lime, cement, fabrica. constr. mat. (excludingglass, clay)
	replace labourintensive=1 if commoditycode==662	// Clay construction, refracto. construction materials
	replace labourintensive=1 if commoditycode==663	// Mineral manufactures, n.e.s.
	replace labourintensive=1 if commoditycode==664	// Glass
	replace labourintensive=1 if commoditycode==665	// Glassware
	replace labourintensive=1 if commoditycode==666	// Pottery
	replace labourintensive=1 if commoditycode==821	// Furniture & parts
	replace labourintensive=1 if commoditycode==831	// Travel goods, handbags & similar containers
	replace labourintensive=1 if commoditycode==841	// Men's clothing of textile fabrics, not knitted
	replace labourintensive=1 if commoditycode==842	// Women's clothing, of textile fabrics
	replace labourintensive=1 if commoditycode==843	// Men's or boy's clothing, of textile, knitted, croche.
	replace labourintensive=1 if commoditycode==844	// Women's clothing, of textile, knitted or crocheted
	replace labourintensive=1 if commoditycode==845	// Articles of apparel, of textile fabrics, n.e.s.
	replace labourintensive=1 if commoditycode==846	// Clothing accessories, of textile fabrics
	replace labourintensive=1 if commoditycode==848	// Articles of apparel, clothing access., excluding textile
	replace labourintensive=1 if commoditycode==851	// Footwear

	label var labourintensive "Labour intensive imports"

	* TDRC	Low-skill and technology-intensive manufactures

	gen lowskill=1 if commoditycode==671			// Pig iron & spiegeleisen, sponge iron, powder & granu
	replace lowskill=1 if commoditycode==672		// Ingots, primary forms, of iron or steel; semi-finis.
	replace lowskill=1 if commoditycode==673		// Flat-rolled prod., iron, non-alloy steel, not coated
	replace lowskill=1 if commoditycode==674		// Flat-rolled prod., iron, non-alloy steel, coated, clad
	replace lowskill=1 if commoditycode==675		// Flat-rolled products of alloy steel
	replace lowskill=1 if commoditycode==676		// Iron & steel bars, rods, angles, shapes & sections
	replace lowskill=1 if commoditycode==677		// Rails & railway track construction mat., iron, steel
	replace lowskill=1 if commoditycode==678		// Wire of iron or steel
	replace lowskill=1 if commoditycode==679		// Tubes, pipes & hollow profiles, fittings, iron, steel
	replace lowskill=1 if commoditycode==691		// Structures & parts, n.e.s., of iron, steel, aluminium
	replace lowskill=1 if commoditycode==692		// Metal containers for storage or transport
	replace lowskill=1 if commoditycode==693		// Wire products (excluding electrical) and fencing grills
	replace lowskill=1 if commoditycode==694		// Nails, screws, nuts, bolts, rivets & the like, of metal
	replace lowskill=1 if commoditycode==695		// Tools for use in the hand or in machine
	replace lowskill=1 if commoditycode==696		// Cutlery
	replace lowskill=1 if commoditycode==697		// Household equipment of base metal, n.e.s.
	replace lowskill=1 if commoditycode==699		// Manufactures of base metal, n.e.s.
	replace lowskill=1 if commoditycode==785		// Motorcycles & cycles
	replace lowskill=1 if commoditycode==786		// Trailers & semi-trailers
	replace lowskill=1 if commoditycode==791		// Railway vehicles & associated equipment
	replace lowskill=1 if commoditycode==793		// Ships, boats & floating structures
	replace lowskill=1 if commoditycode==895		// Office & stationery supplies, n.e.s.
	replace lowskill=1 if commoditycode==899		// Miscellaneous manufactured articles, n.e.s.

	label var lowskill "Low-skill intensive imports"

	* TDRD	Medium-skill and technology-intensive manufactures
	* TDRD1	Medium-skill: Electronics (excluding parts and components) (SITC 775)

	gen mediumskill=1 if commoditycode==775			// Household type equipment, electrical or not, n.e.s.

	* TDRD2	Medium-skill: Parts and components for electrical and electronic goods (SITC 772)

	replace mediumskill=1 if commoditycode==772		// Apparatus for electrical circuits; board, panels

	* TDRD3	Medium-skill: Other, excluding electronics

	replace mediumskill=1 if commoditycode==621		// Materials of rubber (pastes, plates, sheets, etc.)
	replace mediumskill=1 if commoditycode==625		// Rubber tyres, tyre treads or flaps & inner tubes
	replace mediumskill=1 if commoditycode==629		// Articles of rubber, n.e.s.
	replace mediumskill=1 if commoditycode==711		// Vapour generating boilers, auxiliary plant; parts
	replace mediumskill=1 if commoditycode==712		// Steam turbines & other vapour turbin., parts, n.e.s.
	replace mediumskill=1 if commoditycode==713		// Internal combustion piston engines, parts, n.e.s.
	replace mediumskill=1 if commoditycode==714		// Engines & motors, non-electric; parts, n.e.s.
	replace mediumskill=1 if commoditycode==716		// Rotating electric plant & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==718		// Other power generating machinery & parts, n.e.s.
	replace mediumskill=1 if commoditycode==721		// Agricultural machinery (excluding tractors) & parts
	replace mediumskill=1 if commoditycode==722		// Tractors (excluding those of 71414 & 74415)
	replace mediumskill=1 if commoditycode==723		// Civil engineering & contractors' plant & equipment
	replace mediumskill=1 if commoditycode==724		// Textile & leather machinery, & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==725		// Paper mill, pulp mill machinery; paper articles man.
	replace mediumskill=1 if commoditycode==726		// Printing & bookbinding machinery, & parts thereof
	replace mediumskill=1 if commoditycode==727		// Food-processing machines (excluding domestic)
	replace mediumskill=1 if commoditycode==728		// Other machinery for particular industries, n.e.s.
	replace mediumskill=1 if commoditycode==731		// Machine-tools working by removing material
	replace mediumskill=1 if commoditycode==733		// Mach.-tools for working metal, excluding removing mate.
	replace mediumskill=1 if commoditycode==735		// Parts, n.e.s., & accessories for machines of 731, 733
	replace mediumskill=1 if commoditycode==737		// Metalworking machinery (excludingmachine-tools) & parts
	replace mediumskill=1 if commoditycode==741		// Heating & cooling equipment & parts thereof, n.e.s.
	replace mediumskill=1 if commoditycode==742		// Pumps for liquids
	replace mediumskill=1 if commoditycode==743		// Pumps (excluding liquid), gas compressors & fans; centr.
	replace mediumskill=1 if commoditycode==744		// Mechanical handling equipment, & parts, n.e.s.
	replace mediumskill=1 if commoditycode==745		// Other non-electr. machinery, tools & mechan. appar.
	replace mediumskill=1 if commoditycode==746		// Ball or roller bearings
	replace mediumskill=1 if commoditycode==747		// Appliances for pipes, boiler shells, tanks, vats, etc.
	replace mediumskill=1 if commoditycode==748		// Transmis. shafts
	replace mediumskill=1 if commoditycode==749		// Non-electric parts & accessor. of machinery, n.e.s.
	replace mediumskill=1 if commoditycode==771		// Electric power machinery, and parts thereof
	replace mediumskill=1 if commoditycode==773		// Equipment for distributing electricity, n.e.s.
	replace mediumskill=1 if commoditycode==774		// Electro-diagnostic appa. for medical sciences, etc.
	replace mediumskill=1 if commoditycode==778		// Electrical machinery & apparatus, n.e.s.
	replace mediumskill=1 if commoditycode==781		// Motor vehicles for the transport of persons
	replace mediumskill=1 if commoditycode==782		// Motor vehic. for transport of goods, special purpo.
	replace mediumskill=1 if commoditycode==783		// Road motor vehicles, n.e.s.
	replace mediumskill=1 if commoditycode==784		// Parts & accessories of vehicles of 722, 781, 782, 783
	replace mediumskill=1 if commoditycode==811		// Prefabricated buildings
	replace mediumskill=1 if commoditycode==812		// Sanitary, plumbing, heating fixtures, fittings, n.e.s.
	replace mediumskill=1 if commoditycode==813		// Lighting fixtures & fittings, n.e.s.
	replace mediumskill=1 if commoditycode==893		// Articles, n.e.s., of plastics
	replace mediumskill=1 if commoditycode==894		// Baby carriages, toys, games & sporting goods

	label var mediumskill "Medium-skill intensive imports"

	* TDRE	High-skill and technology-intensive manufactures
	* TDRE1	High-skill: Electronics (excluding parts and components) (SITC 751 + 752 + 761 + 762 + 763)

	gen highskill=1 if commoditycode==751		// Office machines
	replace highskill=1 if commoditycode==752	// Automatic data processing machines, n.e.s.
	replace highskill=1 if commoditycode==761	// Television receivers, whether or not combined
	replace highskill=1 if commoditycode==762	// Radio-broadcast receivers, whether or not combined
	replace highskill=1 if commoditycode==763	// Sound recorders or reproducers

	* TDRE2	High-skill: Parts and components for electrical and electronic goods (SITC 759 + 764 + 776)

	replace highskill=1 if commoditycode==759	// Parts, accessories for machines of groups 751, 752
	replace highskill=1 if commoditycode==764	// Telecommunication equipment, n.e.s.; & parts, n.e.s.
	replace highskill=1 if commoditycode==776	// Cathode valves & tubes

	* TDRE3	High-skill: Other, excluding electronics

	replace highskill=1 if commoditycode==511	// Hydrocarbons, n.e.s., & halogenated, nitr. derivative
	replace highskill=1 if commoditycode==512	// Alcohols, phenols, halogenat., sulfonat., nitrat. der.
	replace highskill=1 if commoditycode==513	// Carboxylic acids, anhydrides, halides, per.; derivati.
	replace highskill=1 if commoditycode==514	// Nitrogen-function compounds
	replace highskill=1 if commoditycode==515	// Organo-inorganic, heterocycl. compounds, nucl. acids
	replace highskill=1 if commoditycode==516	// Other organic chemicals
	replace highskill=1 if commoditycode==522	// Inorganic chemical elements, oxides & halogen salts
	replace highskill=1 if commoditycode==523	// Metallic salts & peroxysalts, of inorganic acids
	replace highskill=1 if commoditycode==524	// Other inorganic chemicals
	replace highskill=1 if commoditycode==525	// Radio-actives and associated materials
	replace highskill=1 if commoditycode==531	// Synth. organic colouring matter & colouring lakes
	replace highskill=1 if commoditycode==532	// Dyeing & tanning extracts, synth. tanning materials
	replace highskill=1 if commoditycode==533	// Pigments, paints, varnishes and related materials
	replace highskill=1 if commoditycode==541	// Medicinal and pharmaceutical products, excluding 542
	replace highskill=1 if commoditycode==542	// Medicaments (incl. veterinary medicaments)
	replace highskill=1 if commoditycode==551	// Essential oils, perfume & flavour materials
	replace highskill=1 if commoditycode==553	// Perfumery, cosmetics or toilet prepar. (excluding soaps)
	replace highskill=1 if commoditycode==554	// Soaps, cleansing and polishing preparations
	replace highskill=1 if commoditycode==562	// Fertilizers (other than those of group 272)
	replace highskill=1 if commoditycode==571	// Polymers of ethylene, in primary forms
	replace highskill=1 if commoditycode==572	// Polymers of styrene, in primary forms
	replace highskill=1 if commoditycode==573	// Polymers of vinyl chloride or halogenated olefins
	replace highskill=1 if commoditycode==574	// Polyethers, epoxide resins; polycarbonat., polyesters
	replace highskill=1 if commoditycode==575	// Other plastics, in primary forms
	replace highskill=1 if commoditycode==579	// Waste, parings and scrap, of plastics
	replace highskill=1 if commoditycode==581	// Tubes, pipes and hoses of plastics
	replace highskill=1 if commoditycode==582	// Plates, sheets, films, foil & strip, of plastics
	replace highskill=1 if commoditycode==583	// Monofilaments, of plastics, cross-section > 1mm
	replace highskill=1 if commoditycode==591	// Insectides &  similar products, for retail sale
	replace highskill=1 if commoditycode==592	// Starche, wheat gluten; albuminoidal substances; glues
	replace highskill=1 if commoditycode==593	// Explosives and pyrotechnic products
	replace highskill=1 if commoditycode==597	// Prepared addit. for miner. oils; lubricat., de-icing
	replace highskill=1 if commoditycode==598	// Miscellaneous chemical products, n.e.s.
	replace highskill=1 if commoditycode==792	// Aircraft & associated equipment; spacecraft, etc.
	replace highskill=1 if commoditycode==871	// Optical instruments & apparatus, n.e.s.
	replace highskill=1 if commoditycode==872	// Instruments & appliances, n.e.s., for medical, etc.
	replace highskill=1 if commoditycode==873	// Meters & counters, n.e.s.
	replace highskill=1 if commoditycode==874	// Measuring, analysing & controlling apparatus, n.e.s.
	replace highskill=1 if commoditycode==881	// Photographic apparatus & equipment, n.e.s.
	replace highskill=1 if commoditycode==882	// Cinematographic & photographic supplies
	replace highskill=1 if commoditycode==883	// Cinematograph films, exposed & developed
	replace highskill=1 if commoditycode==884	// Optical goods, n.e.s.
	replace highskill=1 if commoditycode==885	// Watches & clocks
	replace highskill=1 if commoditycode==891	// Arms & ammunition
	replace highskill=1 if commoditycode==892	// Printed matter
	replace highskill=1 if commoditycode==896	// Works of art, collectors' pieces & antiques
	replace highskill=1 if commoditycode==897	// Jewellery & articles of precious materia., n.e.s.
	replace highskill=1 if commoditycode==898	// Musical instruments, parts; records, tapes & similar

	label var highskill "High-skill intensive imports"

	* Create stata_id for each country to match with GALLUP data

	rename country country_gallup

	* Code for matching country names

	gen stata_id=. 	
	replace stata_id=1 		if country_gallup=="Afghanistan"
	replace stata_id=2	 	if country_gallup=="Albania"
	replace stata_id=3		if country_gallup=="Algeria"
	replace stata_id=4		if country_gallup=="Angola"
	replace stata_id=5		if country_gallup=="Argentina"
	replace stata_id=6		if country_gallup=="Armenia"
	replace stata_id=7		if country_gallup=="Australia"
	replace stata_id=8		if country_gallup=="Austria"
	replace stata_id=9		if country_gallup=="Azerbaijan"
	replace stata_id=10		if country_gallup=="Bahamas, The"
	replace stata_id=11		if country_gallup=="Bahrain"
	replace stata_id=12		if country_gallup=="Bangladesh"
	replace stata_id=13		if country_gallup=="Barbados"
	replace stata_id=14		if country_gallup=="Belarus"
	replace stata_id=15		if country_gallup=="Belgium"
	replace stata_id=16		if country_gallup=="Belize"
	replace stata_id=17		if country_gallup=="Benin"
	replace stata_id=18		if country_gallup=="Bhutan"
	replace stata_id=19		if country_gallup=="Bolivia"
	replace stata_id=21		if country_gallup=="Bosnia and Herzegovina" | country_gallup=="Bosnia Herzegovina"
	replace stata_id=22		if country_gallup=="Botswana"
	replace stata_id=23		if country_gallup=="Brazil"
	replace stata_id=24		if country_gallup=="Brunei Darussalam"
	replace stata_id=25		if country_gallup=="Bulgaria"
	replace stata_id=26		if country_gallup=="Burkina Faso"
	replace stata_id=27		if country_gallup=="Burundi"
	replace stata_id=28		if country_gallup=="Cabo Verde"
	replace stata_id=29		if country_gallup=="Cambodia"
	replace stata_id=30		if country_gallup=="Cameroon"
	replace stata_id=31		if country_gallup=="Canada"
	replace stata_id=32		if country_gallup=="Cape Verde"
	replace stata_id=33		if country_gallup=="Central African Republic" | country_gallup=="Central African Rep."
	replace stata_id=34		if country_gallup=="Chad"
	replace stata_id=35		if country_gallup=="Chile"
	replace stata_id=36		if country_gallup=="China"
	replace stata_id=37		if country_gallup=="Colombia"
	replace stata_id=38		if country_gallup=="Comoros"
	replace stata_id=39		if country_gallup=="Congo Brazzaville"
	replace stata_id=40		if country_gallup=="Congo Kinshasa"
	replace stata_id=41		if country_gallup=="Costa Rica"
	replace stata_id=42		if country_gallup=="Croatia"
	replace stata_id=43		if country_gallup=="Cuba"
	replace stata_id=44		if country_gallup=="Cyprus"
	replace stata_id=45		if country_gallup=="Czech Republic"
	replace stata_id=46		if country_gallup=="Denmark"
	replace stata_id=47		if country_gallup=="Djibouti"
	replace stata_id=48		if country_gallup=="Dominican Republic" | country_gallup=="Dominican Rep."
	replace stata_id=49		if country_gallup=="East Timor"
	replace stata_id=50		if country_gallup=="Ecuador"
	replace stata_id=51		if country_gallup=="Egypt"
	replace stata_id=52		if country_gallup=="El Salvador"
	replace stata_id=53		if country_gallup=="Equatorial Guinea"
	replace stata_id=54		if country_gallup=="Eritrea"
	replace stata_id=55		if country_gallup=="Estonia"
	replace stata_id=56		if country_gallup=="Ethiopia"
	replace stata_id=58		if country_gallup=="Fiji"
	replace stata_id=59		if country_gallup=="Finland"
	replace stata_id=60		if country_gallup=="France"
	replace stata_id=61		if country_gallup=="Gabon"
	replace stata_id=62		if country_gallup=="Gambia"
	replace stata_id=63		if country_gallup=="Georgia"
	replace stata_id=64		if country_gallup=="Germany"
	replace stata_id=65		if country_gallup=="Ghana"
	replace stata_id=66		if country_gallup=="Greece"
	replace stata_id=67		if country_gallup=="Guatemala"
	replace stata_id=68		if country_gallup=="Guinea"
	replace stata_id=70		if country_gallup=="Guyana"
	replace stata_id=71		if country_gallup=="Haiti"
	replace stata_id=72		if country_gallup=="Honduras"
	replace stata_id=73		if country_gallup=="Hong Kong"
	replace stata_id=75		if country_gallup=="Hungary"
	replace stata_id=76		if country_gallup=="Iceland"
	replace stata_id=77		if country_gallup=="India"
	replace stata_id=78		if country_gallup=="Indonesia"
	replace stata_id=79		if country_gallup=="Iran"
	replace stata_id=80		if country_gallup=="Iraq"
	replace stata_id=81		if country_gallup=="Ireland"
	replace stata_id=82		if country_gallup=="Israel"
	replace stata_id=83		if country_gallup=="Italy"
	replace stata_id=84		if country_gallup=="Ivory Coast" | country_gallup=="CÃ´te d'Ivoire"
	replace stata_id=85		if country_gallup=="Jamaica"
	replace stata_id=86		if country_gallup=="Japan"
	replace stata_id=87		if country_gallup=="Jordan"
	replace stata_id=88		if country_gallup=="Kazakhstan"
	replace stata_id=89		if country_gallup=="Kenya"
	replace stata_id=91		if country_gallup=="Korea North"
	replace stata_id=92		if country_gallup=="Kosovo"
	replace stata_id=93		if country_gallup=="Kuwait"
	replace stata_id=95		if country_gallup=="Kyrgyzstan" | country_gallup=="Kyrgyz Republic"
	replace stata_id=96		if country_gallup=="Laos"
	replace stata_id=97		if country_gallup=="Latvia"
	replace stata_id=98		if country_gallup=="Lebanon"
	replace stata_id=99		if country_gallup=="Lesotho"
	replace stata_id=100	if country_gallup=="Liberia"
	replace stata_id=101	if country_gallup=="Libya"
	replace stata_id=102	if country_gallup=="Lithuania"
	replace stata_id=103	if country_gallup=="Luxembourg"
	replace stata_id=104	if country_gallup=="Macao SAR"
	replace stata_id=106	if country_gallup=="Macedonia, FYR"
	replace stata_id=107	if country_gallup=="Madagascar"
	replace stata_id=108	if country_gallup=="Malawi"
	replace stata_id=109	if country_gallup=="Malaysia"
	replace stata_id=110	if country_gallup=="Mali"
	replace stata_id=111	if country_gallup=="Malta"
	replace stata_id=112	if country_gallup=="Mauritania"
	replace stata_id=113	if country_gallup=="Mauritius"
	replace stata_id=114	if country_gallup=="Mexico"
	replace stata_id=115	if country_gallup=="Moldova"
	replace stata_id=116	if country_gallup=="Mongolia"
	replace stata_id=117	if country_gallup=="Montenegro"
	replace stata_id=118	if country_gallup=="Morocco"
	replace stata_id=119	if country_gallup=="Mozambique"
	replace stata_id=120	if country_gallup=="Myanmar"
	replace stata_id=122	if country_gallup=="Nagorno Karabakh"
	replace stata_id=123	if country_gallup=="Namibia"
	replace stata_id=124	if country_gallup=="Nepal"
	replace stata_id=125	if country_gallup=="Netherlands"
	replace stata_id=126	if country_gallup=="New Zealand"
	replace stata_id=127	if country_gallup=="Nicaragua"
	replace stata_id=128	if country_gallup=="Niger"
	replace stata_id=129	if country_gallup=="Nigeria"
	replace stata_id=130	if country_gallup=="Northern Cyprus"
	replace stata_id=131	if country_gallup=="Norway"
	replace stata_id=132	if country_gallup=="Oman"
	replace stata_id=133	if country_gallup=="Pakistan"
	replace stata_id=134	if country_gallup=="Palestine" | country_gallup=="State of Palestine"
	replace stata_id=135	if country_gallup=="Panama"
	replace stata_id=136	if country_gallup=="Papua New Guinea"
	replace stata_id=137	if country_gallup=="Paraguay"
	replace stata_id=138	if country_gallup=="Peru"
	replace stata_id=139	if country_gallup=="Philippines"
	replace stata_id=140	if country_gallup=="Poland"
	replace stata_id=141	if country_gallup=="Portugal"
	replace stata_id=142	if country_gallup=="Puerto Rico"
	replace stata_id=143	if country_gallup=="Qatar"
	replace stata_id=144	if country_gallup=="Romania"
	replace stata_id=145	if country_gallup=="Russia"
	replace stata_id=146	if country_gallup=="Rwanda"
	replace stata_id=147	if country_gallup=="San Marino"
	replace stata_id=148	if country_gallup=="Saudi Arabia"
	replace stata_id=149	if country_gallup=="Senegal"
	replace stata_id=150	if country_gallup=="Serbia"
	replace stata_id=151	if country_gallup=="Serbia and Montenegro"
	replace stata_id=152	if country_gallup=="Seychelles"
	replace stata_id=153	if country_gallup=="Sierra Leone"
	replace stata_id=154	if country_gallup=="Singapore"
	replace stata_id=155	if country_gallup=="Slovak Republic"
	replace stata_id=157	if country_gallup=="Slovenia"
	replace stata_id=158	if country_gallup=="Solomon Islands"
	replace stata_id=159	if country_gallup=="Somalia"
	replace stata_id=161	if country_gallup=="South Africa"
	replace stata_id=162	if country_gallup=="South Korea" | country_gallup=="Rep. of Korea"
	replace stata_id=163	if country_gallup=="South Sudan"
	replace stata_id=164	if country_gallup=="Spain"
	replace stata_id=165	if country_gallup=="Sri Lanka"
	replace stata_id=166	if country_gallup=="Sudan"
	replace stata_id=168	if country_gallup=="Suriname"
	replace stata_id=169	if country_gallup=="Swaziland"
	replace stata_id=170	if country_gallup=="Sweden"
	replace stata_id=171	if country_gallup=="Switzerland"
	replace stata_id=172	if country_gallup=="Syria"
	replace stata_id=173	if country_gallup=="S䯠Tom顡nd Pripe"
	replace stata_id=174	if country_gallup=="Taiwan"
	replace stata_id=175	if country_gallup=="Taiwan Province of China"
	replace stata_id=176	if country_gallup=="Tajikistan"
	replace stata_id=177	if country_gallup=="Tanzania" | country_gallup=="United Rep. of Tanzania"
	replace stata_id=178	if country_gallup=="Thailand"
	replace stata_id=179	if country_gallup=="Togo"
	replace stata_id=180	if country_gallup=="Trinidad and Tobago"
	replace stata_id=181	if country_gallup=="Tunisia"
	replace stata_id=182	if country_gallup=="Turkey"
	replace stata_id=183	if country_gallup=="Turkmenistan"
	replace stata_id=184	if country_gallup=="UAE"
	replace stata_id=185	if country_gallup=="Uganda"
	replace stata_id=186	if country_gallup=="Ukraine"
	replace stata_id=187	if country_gallup=="United Arab Emirates"
	replace stata_id=188	if country_gallup=="United Kingdom" 
	replace stata_id=189	if country_gallup=="United States of America" | country_gallup=="USA"
	replace stata_id=190	if country_gallup=="Uruguay"
	replace stata_id=191	if country_gallup=="Uzbekistan"
	replace stata_id=192	if country_gallup=="Venezuela"
	replace stata_id=193	if country_gallup=="Viet Nam"
	replace stata_id=194	if country_gallup=="Yemen"
	replace stata_id=195	if country_gallup=="Zambia"
	replace stata_id=196	if country_gallup=="Zimbabwe"

	* Detect unmatched stata_id and country names

	tab country_gallup if stata_id==.

	replace stata_id=10		if country_gallup=="Bahamas"
	replace stata_id=19		if country_gallup=="Bolivia (Plurinational State of)"
	replace stata_id=39		if country_gallup=="Congo"
	replace stata_id=40		if country_gallup=="Democratic Republic of the Congo"
	replace stata_id=84		if country_gallup=="Côte d'Ivoire"
	replace stata_id=51		if country_gallup=="Egypt, Arab Rep."
	replace stata_id=62		if country_gallup=="Gambia, The"
	replace stata_id=68		if country_gallup=="Guinea-Bissau"
	replace stata_id=73		if country_gallup=="China, Hong Kong SAR"
	replace stata_id=79		if country_gallup=="Iran (Islamic Republic of)"
	replace stata_id=91		if country_gallup=="Dem. People's Republic of Korea"
	replace stata_id=162	if country_gallup=="Republic of Korea"
	replace stata_id=96		if country_gallup=="Lao People's Democratic Republic"
	replace stata_id=104	if country_gallup=="China, Macao SAR"
	replace stata_id=106	if country_gallup=="TFYR Macedonia" | country_gallup=="TFYR of Macedonia"
	replace stata_id=115	if country_gallup=="Rep. of Moldova"
	replace stata_id=145	if country_gallup=="Russian Federation"
	replace stata_id=155	if country_gallup=="Slovakia"
	replace stata_id=173	if country_gallup=="Sao Tome and Principe"
	replace stata_id=172	if country_gallup=="Syrian Arab Republic"
	replace stata_id=192	if country_gallup=="Venezuela (Bolivarian Republic of)"
	replace stata_id=194	if country_gallup=="Yemen, Rep."

	tab stata_id

	drop if stata_id==.
	
	save "$data/Trade_data_cleaned.dta", replace

	******************************************************
	* Calculate annual import volumes by skill composition
	******************************************************

	* Labour intensive imports

	collapse (sum) importvalue if labourintensive==1, by (country_gallup year stata_id)

	rename importvalue total_labour_int_exp
	label var total_labour_int_exp "Total Labour Intensive Imports" 

	save "$data/2002_2018_labourintensive_imports_country_level.dta", replace

	* Low-skill intensive imports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) importvalue if lowskill==1, by (country_gallup year stata_id)

	rename importvalue total_lowskill_int_exp
	label var total_lowskill_int_exp "Total Low-Skill Intensive Imports" 

	save "$data/2002_2018_lowskilled_imports_country_level.dta", replace

	* Medium-skill intensive imports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) importvalue if mediumskill==1, by (country_gallup year stata_id)

	rename importvalue total_mediumskill_int_exp
	label var total_mediumskill_int_exp "Total Medium-Skill Intensive Imports" 

	save "$data/2002_2018_mediumskilled_imports_country_level.dta", replace

	* High-skill intensive imports

	use "$data/Trade_data_cleaned.dta", clear

	collapse (sum) importvalue if highskill==1, by (country_gallup year stata_id)

	rename importvalue total_highskill_int_exp
	label var total_highskill_int_exp "Total High-Skill Intensive Imports" 

	save "$data/2002_2018_highskilled_imports_country_level.dta", replace


****************************************************************
**# Construct Instruments with Pair Fixed Effects 
****************************************************************

	* Exports

	global trade = "highskilled mediumskilled lowskilled labourintensive"

	foreach k in $trade {

	use "$data/2002_2018_`k'_exports_country_level_v1.dta", clear

	set more off
	set matsize 10000, perm 
	set emptycells drop

	* Generate dummies

	tab year, gen (yeardum)
	tab stata_id, gen (exporterdum)
	tab stata_id_partner, gen (importerdum)

	* Generate distance-year interactions

	forvalues i = 1(1)17 {
	gen l_seadist_shrt_`i'=l_seadist_shrt*yeardum`i'
	}

	forvalues i = 1(1)17 {
	gen l_distw_`i'=l_distw*yeardum`i'
	}


	* 1-) Estimate total skilled exports with OLS

	areg l_value l_seadist_shrt_* l_distw_* yeardum* comlang contig evercol, robust absorb(pair_id)

	predict ln_ex1_pair_`k' if e(sample), xbd // e(sample) only adds fitted values back onto obs in the regression, xbd includes the fitted values and the observed FE

	g IV_export_1_pair_`k'=exp(ln_ex1_pair_`k')

	rename l_value l_value_`k'_export

	sum ln_ex1_pair_`k' IV_export_1_pair_`k' `k'_exports l_value_`k'_export

	collapse (sum) ln_ex1_pair_`k' IV_export_1_pair_`k' l_value_`k'_export `k'_exports , by(stata_id year)    

	drop if stata_id==.

	* Replace zeros to missing

	foreach var of varlist IV_export_1_pair_`k' {
	replace `var'=. if `var'==0
	}

	label var ln_ex1_pair_`k' "Fitted value, 1 Exports_`k'"
	label var IV_export_1_pair_`k' "Export IV 1 - OLS - Pair FEs_`k'"
	label var l_value_`k'_export "Sum log Exports `k'"
	label var `k'_exports "Exports `k'"


	save "$data/IV_`k'_exports.dta", replace 
	}


	* Imports:

	foreach k in $trade {

	use "$data/2002_2018_`k'_imports_country_level_v1.dta", clear

	set more off
	set matsize 10000, perm 
	set emptycells drop

	* Generate dummies

	tab year, gen (yeardum)
	tab stata_id, gen (exporterdum)
	tab stata_id_partner, gen (importerdum)

	* Generate distance-year interactions

	forvalues i = 1(1)17 {
	gen l_seadist_shrt_`i'=l_seadist_shrt*yeardum`i'
	}

	forvalues i = 1(1)17 {
	gen l_distw_`i'=l_distw*yeardum`i'
	}


	* 1-) Estimate total skilled exports with OLS

	areg l_value l_seadist_shrt_* l_distw_* yeardum* comlang contig evercol, robust absorb(pair_id)

	predict ln_im1_pair_`k' if e(sample), xbd // e(sample) only adds fitted values back onto obs in the regression, xbd includes the fitted values and the observed FE

	g IV_import_1_pair_`k'=exp(ln_im1_pair_`k')

	rename l_value l_value_`k'_import

	sum ln_im1_pair_`k' IV_import_1_pair_`k' `k'_imports l_value_`k'_import

	collapse (sum) ln_im1_pair_`k' IV_import_1_pair_`k' l_value_`k'_import `k'_imports , by(stata_id year)    

	drop if stata_id==.

	* Replace zeros to missing

	foreach var of varlist IV_import_1_pair_`k' {
	replace `var'=. if `var'==0
	}

	label var ln_im1_pair_`k' "Fitted value, 1 Imports_`k'"
	label var IV_import_1_pair_`k' "Import IV 1 - OLS - Pair FEs_`k'"
	label var l_value_`k'_import "Sum log Imports `k'"
	label var `k'_imports "Imports `k'"


	save "$data/IV_`k'_imports.dta", replace 
	}



	***** Combine exports and imports by skill to get the complete dataset:
	use "$data/IV_highskilled_exports.dta", clear

	merge 1:1 year stata_id using "$data/IV_mediumskilled_exports.dta"
	drop _merge

	merge 1:1 year stata_id using "$data/IV_lowskilled_exports.dta"
	drop _merge

	merge 1:1 year stata_id using "$data/IV_labourintensive_exports.dta"
	drop _merge

	xtset stata_id year

	gen l_high_export_1 = log(highskilled_exports + mediumskilled_exports)
	gen l_low_export_1 = log(lowskilled_exports + labourintensive_exports)

	gen Dif_l_high_export_1 = D.l_high_export_1
	gen Dif_l_low_export_1 = D.l_low_export_1

	gen l_high_export_IV_1 = log(IV_export_1_pair_highskilled + IV_export_1_pair_mediumskilled)
	gen l_low_export_IV_1 = log(IV_export_1_pair_lowskilled + IV_export_1_pair_labourintensive)

	gen Dif_l_high_export_IV_1 = D.l_high_export_IV_1
	gen Dif_l_low_export_IV_1 = D.l_low_export_IV_1

	save "$data/2002_2018_Exports_withIV.dta", replace

	use "$data/IV_highskilled_imports.dta", clear

	merge 1:1 year stata_id using "$data/IV_mediumskilled_imports.dta"
	drop _merge

	merge 1:1 year stata_id using "$data/IV_lowskilled_imports.dta"
	drop _merge

	merge 1:1 year stata_id using "$data/IV_labourintensive_imports.dta"
	drop _merge

	xtset stata_id year

	gen l_high_import_1 = log(highskilled_imports + mediumskilled_imports)
	gen l_low_import_1 = log(lowskilled_imports + labourintensive_imports)

	gen Dif_l_high_import_1 = D.l_high_import_1
	gen Dif_l_low_import_1 = D.l_low_import_1

	gen l_high_import_IV_1 = log(IV_import_1_pair_highskilled + IV_import_1_pair_mediumskilled)
	gen l_low_import_IV_1 = log(IV_import_1_pair_lowskilled + IV_import_1_pair_labourintensive)

	gen Dif_l_high_import_IV_1 = D.l_high_import_IV_1
	gen Dif_l_low_import_IV_1 = D.l_low_import_IV_1

	save "$data/2002_2018_Imports_withIV.dta", replace



